package com.leoman.controller.api;

import com.leoman.core.ErrorType;
import com.leoman.core.bean.Result;
import com.leoman.entity.Post;
import com.leoman.exception.ParamErrorException;
import com.leoman.exception.UserNotFindException;
import com.leoman.exception.UserSealStatusException;
import com.leoman.service.PostService;
import com.leoman.utils.JsonUtil;
import com.leoman.utils.WebUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

/**
 * 帖子相关api
 * Created by 涂奕恒 on 2016/10/19 0018.
 */
@Controller
@RequestMapping(value = "api/post")
public class PostApi {

    @Autowired
    private PostService postService;

    /**
     * @api {post} /api/post/list 1、帖子列表
     * @apiVersion 0.0.1
     * @apiName post.list
     * @apiGroup post
     *
     * @apiDescription 帖子列表
     *
     * @apiParam   {Integer}   userId                     用户Id
     * @apiParam   {Integer}   pageNum                    页码
     * @apiParam   {Integer}   pageSize                   每页显示条数
     *
     * @apiSuccess {Object}    list
     * @apiSuccess {Integer}   list.id                    帖子ID
     * @apiSuccess {Integer}   list.authorName            发帖人昵称
     * @apiSuccess {Integer}   list.authorAvatar          发帖人头像
     * @apiSuccess {String}    list.content               帖子内容
     * @apiSuccess {String}    list.postImageList         帖子图片集合
     * @apiSuccess {String}    list.postImageList.id      帖子图片ID
     * @apiSuccess {String}    list.postImageList.url     帖子图片URL
     * @apiSuccess {Integer}   list.createTime            发帖时间
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status":0,
     *      "msg":"",
     *      "data":{
     *          "page": {
     *              "totalNum": 0,
     *              "totalPage": 0,
     *              "currentPage": 1
     *          },
     *          "list": [
     *              {
     *                  "id" : 1,
     *                  "authorName" : "张雨生",
     *                  "authorAvatar" : "http://4.16.3.22:8080/images/cover.jpg",
     *                  "content" : "因特网",
     *                  "postImageList" : [
     *                      {
     *                          "id" : 2,
     *                          "url" : "http://4.16.3.22:8080/images/cover.jpg"
     *                      }
     *                  ],
     *                  "createTime" : "1476843673494"
     *              }
     *          ]
     *      }
     * }
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void typeList(HttpServletResponse response, Integer userId, Integer pageNum, Integer pageSize) {
        Page<Post> page = postService.iPageForApp(userId, pageNum, pageSize);
        Map<String, Object> dataMap = JsonUtil.fitting(page);
        String result = JsonUtil.obj2ApiJson(Result.successApi(dataMap), "post", "appUser");
        WebUtil.printApi(response, result);
    }

    /**
     * @api {post} /api/post/sendPost 2、用户发帖
     * @apiVersion 0.0.1
     * @apiName post.sendPost
     * @apiGroup post
     *
     * @apiDescription 用户发帖
     *
     * @apiParam {Integer}   userId           用户ID                           (必填)
     * @apiParam {String}    content          帖子内容
     * @apiParam {Integer}   imgNum           图片数量
     * @apiParam {Stream}    file             图片文件名
     *
     * @apiSuccessExample {json} Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "status": 0,
     *      "msg": "",
     *      "data": {}
     * }
     */
    @RequestMapping(value = "/sendPost", method = RequestMethod.POST)
    public void sendPost(HttpServletResponse response, Integer userId, String content, MultipartRequest multipartRequest, Integer imgNum) {
        try {
            if (StringUtils.isNotEmpty(content)) {
                content = URLDecoder.decode(content, "UTF-8");
            }
            postService.sendPost(userId, content, multipartRequest, imgNum);
            WebUtil.printApi(response, JsonUtil.obj2ApiJson(Result.successApi()));
        } catch (ParamErrorException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_NOT_PARAM));
        } catch (UserNotFindException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_USER_NOT_FIND));
        } catch (UnsupportedEncodingException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_BASE));
        } catch (UserSealStatusException e) {
            WebUtil.printApi(response, Result.failureApi(ErrorType.ERROR_USER_STATUS_DISABLED));
        }
    }
}
